package com.zhang.common.message;

import com.zhang.common.Constant;
import com.zhang.common.until.ProtoStuffUtil;

/**
 * 自定义协议工具类封装
 * @author zhanghao
 */
public class ProxyMessageUtil {
    /**
     * 私有化构造器
     */
    private ProxyMessageUtil(){}

    /**
     * 字节数组转实体
     * @param data
     * @return
     */
    public static ProxyMessage decode(byte[] data){
        return ProtoStuffUtil.deserialize(data,ProxyMessage.class);
    }

    /**
     * 实体转字节数组
     * @param proxyMessage
     * @return
     */
    public static byte[] encode(ProxyMessage proxyMessage){
        return ProtoStuffUtil.serialize(proxyMessage);
    }

    /**
     * 构建自定义协议数据
     * @param httpId
     * @param msgType
     * @param port
     * @param domain
     * @param data
     * @return
     */
    public static ProxyMessage buildMsg(Long httpId,Byte msgType,Integer port,String domain,byte[] data,Byte code){
        ProxyMessage proxyMessage = new ProxyMessage();
        proxyMessage.setMsgType(msgType);
        proxyMessage.setHttpId(httpId);
        proxyMessage.setPort(port);
        proxyMessage.setDomain(domain);
        proxyMessage.setData(data);
        proxyMessage.setCode(code);
        return proxyMessage;
    }

    /**
     * 客户端注册域名
     * @param domain
     * @return
     */
    public static ProxyMessage buildRegisterReq(String domain){
        return buildMsg(
                null,
                Constant.MessageType.TYPE_REGISTER_REQUEST,
                null,
                domain,
                null,
                null
        );
    }

    /**
     * 构建 注册响应消息
     * @param code   需要填写错误类型
     * @return
     */
    public static ProxyMessage buildRegisterRespFail(byte code){
        return buildMsg(null,
                Constant.MessageType.TYPE_REGISTER_RESPONSE_FAIL,
                null,null,null,code
                );
    }

    /**
     * 构建注册成功响应
     * @return
     */
    public static ProxyMessage buildRegisterRespSuccess(Integer port){
        return buildMsg(null,
                Constant.MessageType.TYPE_REGISTER_RESPONSE_SUCCESS,
                port,null,null,null
        );
    }

    /**
     * 代理服务器 构建传输请求
     * @param httpId  浏览器跟代理服务器 channel 的绑定的会话id
     * @param data 浏览器的请求
     * @return
     */
    public static ProxyMessage buildTransferReq(Long httpId,byte[] data){
        return buildMsg(
                httpId,
                Constant.MessageType.TYPE_TRANSFER_REQUEST,
                null,
                null,
                data,
                null
        );
    }

    /**
     * 代理客户端响应请求 传给代理服务器
     * @param httpId
     * @param data
     * @return
     */
    public static ProxyMessage buildTransferRespSuccess(Long httpId,byte[] data){
        return buildMsg(
                httpId,
                Constant.MessageType.TYPE_TRANSFER_RESPONSE_SUCCESS,
                null,
                null,
                data,
                null
        );
    }

    /**
     * 构建 传输真实响应失败 消息
     * @param httpId
     * @return
     */
    public static ProxyMessage buildTransferRespFail(Long httpId){
        return buildMsg(
                httpId,
                Constant.MessageType.TYPE_TRANSFER_RESPONSE_FAIL,
                null,null,null,null
                );
    }
    /**
     * 代理服务器构建 连接真实服务器请求
     * @return
     */
    public static ProxyMessage buildConnectRealServerReq(Long httpId){
        return buildMsg(
            httpId,
            Constant.MessageType.TYPE_CONNECT_REAL_SERVER_REQUEST,
                null,null,null,null
        );
    }

    /**
     * 构建连接真实服务器响应成功消息
     * @param httpId
     * @return
     */
    public static ProxyMessage buildConnectRealServerRespSuccess(Long httpId){
        return buildMsg(
                httpId,
                Constant.MessageType.TYPE_CONNECT_REAL_SERVER_RESPONSE_SUCCESS,
                null,null,null,null
        );
    }

    /**
     * 构建连接真实服务器响应失败消息
     * @param httpId
     * @return
     */
    public static ProxyMessage buildConnectRealServerRespFail(Long httpId){
        return buildMsg(httpId,
                Constant.MessageType.TYPE_CONNECT_REAL_SERVER_RESPONSE_FAIL,
                null,null,null,null);
    }

    /**
     * 构建真实服务器断开连接请求
     * @return
     */
    public static ProxyMessage buildRealServerCloseResp(Long httpId){
        return buildMsg(httpId,
                Constant.MessageType.TYPE_CONNECT_REAL_SERVER_CLOSE_RESPONSE,
                null,null,null,null);
    }

    /**
     * 构建心跳响应
     * @return
     */
    public static ProxyMessage buildHeartBeatResp(){
        return buildMsg(null,Constant.HeartBeat.TYPE_HEART_BEAT_RESPONSE,
                null,null,null,null);
    }
}
